﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SpamMail.Source.App.Classes;
using System.Data.OleDb;
using System.Threading;

namespace SpamMail
{
    public partial class FormSendMail : Form
    {
        public FormSendMail()
        {
            InitializeComponent();
            lwMail.CheckBoxes = true;
        }

        private void FormSendMail_Load(object sender, EventArgs e)
        {
            SendMail();
        }

        public void SendMail()
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnImportMail_Click(object sender, EventArgs e)
        {
            oFdImportMail.ShowDialog();
        }

        private void oFdImportMail_FileOk(object sender, CancelEventArgs e)
        {
            string strFilename = oFdImportMail.FileName;
            string conn = ("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strFilename + ";" + "Extended Properties=Excel 8.0;");
            string sql = "select * from [Sheet1$]";
            OleDbDataAdapter objAdapter = new OleDbDataAdapter(sql, conn);
            DataSet objDS = new DataSet();
            objAdapter.Fill(objDS);
            DataTable objDT = objDS.Tables[0];
            foreach (DataRow row in objDT.Rows)
            {
                ListViewItem lwEmailItem = new ListViewItem();
                lwEmailItem.SubItems.Add(row[0].ToString());
                lwMail.Items.Add(lwEmailItem);
            }

        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Mail.Common common = new Mail.Common();
            System.Windows.Forms.ListView.CheckedListViewItemCollection Items = lwMail.CheckedItems;
            if (Items.Count > 0)
            {
                ListViewItem lwLoggingItem = new ListViewItem("Bắt đầu gửi mail: " + DateTime.Now);
                lwLogging.Items.Add(lwLoggingItem);
                lwLogging.Refresh();
                //string subject = txtSubject.Text;
                //string content = rtbMailContent.Text;
                //foreach (ListViewItem item in Items)
                //{
                //    ThreadStart actionThread = delegate { MailTo(item.SubItems[1].Text, subject, content); };
                //    //CheckForIllegalCrossThreadCalls = false;
                //    new Thread(actionThread).Start();
                //}

            }
            else
            {
                MessageBox.Show("Chọn Email trước khi gửi!");
                return;
            }
            int counter = 0;
            DateTime objStart = DateTime.Now;
            string subject = txtSubject.Text;
            string content = rtbMailContent.Text;
            foreach (ListViewItem item in Items)
            {
                counter++;
                string mail = item.SubItems[1].Text;
                ListViewItem lwLoggingItem = new ListViewItem("Sending Email: " + mail + " - " + DateTime.Now);
                lwLoggingItem.SubItems.Add("Sending Email: " + mail + " - " + DateTime.Now);
                lwLogging.Items.Add(lwLoggingItem);
                lwLogging.Refresh();
                lwLogging.EnsureVisible(lwLogging.Items.Count - 1);
                common.SendMail(mail, "Hai Ngoc", subject, content);
                lwLoggingItem = new ListViewItem("Sent - " + mail + " - " + DateTime.Now);
                lwLogging.Items.Add(lwLoggingItem);
                lwLoggingItem = new ListViewItem("---------------");
                lwLogging.Items.Add(lwLoggingItem);
                lwLogging.Refresh();
                lwLogging.EnsureVisible(lwLogging.Items.Count - 1);
            }
            DateTime objEnd = DateTime.Now;
            ListViewItem lwLoggingI = new ListViewItem("Hoàn thành: " + (objEnd - objStart).TotalSeconds.ToString() + "s | Trung bình: " + ((objEnd - objStart).TotalSeconds / counter).ToString("#0") + "s/email");
            lwLogging.Items.Add(lwLoggingI);
            lwLogging.Refresh();
            lwLogging.EnsureVisible(lwLogging.Items.Count - 1);
        }

        protected void MailTo(string mail, string subject, string content)
        {
            EmailFunctions function = new EmailFunctions();
            function.To = mail;
            function.Content = content;
            function.Subject = subject;
            function.Start = 0;
            function.End = 1;
            function.Send();
            AddLw(mail);
        }

        delegate void SetTextCallback(string text);

        protected void AddLw(string mail)
        {
            if (this.lwLogging.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(AddLw);
                this.Invoke(d, new object[] { mail });
            }
            else
            {
                ListViewItem lwLoggingItem = new ListViewItem("Email: " + mail + " - " + DateTime.Now);
                lwLogging.Items.Add(lwLoggingItem);
            }

        }
    }
}
